<template>
  <div class="bj">
    <div class="body">
      <div class="clearfloat">
        <div id="search">
          <a-select :allowClear="true" placeholder="请选择用户状态" v-model="searchForm.status">
            <a-select-option value="0">冻结</a-select-option>
            <a-select-option value="1">激活</a-select-option>
            <a-select-option value>全部</a-select-option>
          </a-select>
          <a-select :allowClear="true" placeholder="请选择用户会员等级" v-model="searchForm.level">
            <a-select-option value="0">普通会员</a-select-option>
            <a-select-option value="1">VIP会员</a-select-option>
            <a-select-option value>全部</a-select-option>
          </a-select>
          <a-select :allowClear="true" placeholder="请选择用户性别" v-model="searchForm.gender">
            <a-select-option value="1">女</a-select-option>
            <a-select-option value="2">男</a-select-option>
            <a-select-option value>全部</a-select-option>
          </a-select>
          <a-input placeholder="请输入用户ID" v-model="searchForm.id" />
          <a-input placeholder="请输入用户昵称" v-model="searchForm.nickname" />
          <a-button type="primary" icon="search" @click="search">查找</a-button>
        </div>
      </div>
      <a-table
        ref="table"
        size="default"
        rowKey="id"
        :pagination="pagination"
        :columns="memberColumns"
        :dataSource="loadData"
        :loading="loading"
        @change="pageChange"
      >
        <span slot="gender" slot-scope="text">
          <a-tag color="blue" v-if="text === 1">男</a-tag>
          <a-tag color="blue" v-if="text === 2">女</a-tag>
        </span>
        <span slot="level" slot-scope="text">
          <a-tag color="blue" v-if="text === 0">普通会员</a-tag>
          <a-tag color="blue" v-if="text === 1">白银会员</a-tag>
          <a-tag color="blue" v-if="text === 2">黄金会员</a-tag>
          <a-tag color="blue" v-if="text === 3">铂金会员</a-tag>
          <a-tag color="blue" v-if="text === 4">砖石会员</a-tag>
        </span>
        <span slot="status" slot-scope="text">
          <a-tag color="blue" v-if="text === 1">激活</a-tag>
          <a-tag color="blue" v-if="text === 0">冻结</a-tag>
        </span>
        <span slot="gmtLastLogin" slot-scope="text,record">
          <span v-if="record.gmtLastLogin">{{ text | formDate }}</span>
        </span>
        <div slot="action" slot-scope="text,record">
          <a-button
            type="primary"
            class="flyray-btn"
            v-show="record.status == 0"
            @click="activState(record)"
          >激活</a-button>
          <a-button
            type="danger"
            class="table-danger flyray-btn"
            v-show="record.status == 1"
            @click="frozenState(record)"
          >冻结</a-button>
        </div>
      </a-table>
    </div>
  </div>
</template>
<script>
import { getList, avtieStatus, frozenStatus } from '@/api/mall/member'
import { memberColumns } from '@/const/crud/mall/member.config'
export default {
  data() {
    return {
      memberColumns,
      loading: true,
      parameter: {
        //列表参数
        pageNo: 1, //当前页码
        pageSize: 10 //页码长度
      },
      searchForm: {
        //搜索参数
        nickname: '', //用户昵称
        id: null, //用户id
        level: undefined, //用户等级
        gender: undefined, //用户性别
        status: undefined //用户状态
      },
      pagination: {
        //分页参数
        total: 0,
        pageSize: 10,
        showTotal: total => `共有 ${total} 条数`,
        showSizeChanger: true,
        pageSizeOptions: ['10', '20', '30', '40'],
        onShowSizeChange: (current, pageSize) => (this.pageSize = pageSize)
      },
      statusForm: {
        // 状态参数
        userId: null,
        status: null
      },
      loadData: [],
      visible: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getMemberlist() {
      //获取会员列表
      getList(Object.assign(this.parameter)).then(res => {
        this.loadData = res.data.items
        this.loading = false
        this.pagination.total = res.data.total
      })
    },
    search() {
      //查找
      this.parameter.nickname = this.searchForm.nickname //昵称
      this.parameter.id = this.searchForm.id //id
      this.parameter.level = this.searchForm.level //等级
      this.parameter.gender = this.searchForm.gender //性别
      this.parameter.status = this.searchForm.status //状态
      this.getMemberlist()
    },
    // 激活
    activState(val) {
      this.statusForm.userId = val.id
      this.statusForm.status = val.status
      avtieStatus(Object.assign(this.statusForm)).then(res => {
        if (res.errno === 200) {
          this.getMemberlist()
          this.$notification.open({
            message: '成功',
            description: '状态更改成功',
            icon: <a-icon type="check-circle" style="color: #67c23a" />
          })
        } else {
          this.$notification.open({
            message: '失败',
            description: '状态更改失败',
            icon: <a-icon type="close-circle" style="color: red" />
          })
        }
      })
    },
    // 冻结
    frozenState(val) {
      this.statusForm.userId = val.id
      this.statusForm.status = val.status
      frozenStatus(Object.assign(this.statusForm)).then(res => {
        if (res.errno === 200) {
          this.getMemberlist()
          this.$notification.open({
            message: '成功',
            description: '状态更改成功',
            icon: <a-icon type="check-circle" style="color: #67c23a" />
          })
        } else {
          this.$notification.open({
            message: '失败',
            description: '状态更改失败',
            icon: <a-icon type="close-circle" style="color: red" />
          })
        }
      })
    },
    pageChange(pagination) {
      //分页
      this.parameter.pageNo = pagination.current //点击的页码
      this.parameter.pageSize = pagination.pageSize //一页显示多少条
      this.getMemberlist()
    }
  }
}
</script>
<style lang="less" scoped>
.body {
  background-color: #ffffff;
  padding: 24px 32px;
}
.flyray-btn {
  font-size: 12px;
  padding: 0 7px;
  margin: 0 0 0 3px;
  height: 24px;
}
.table-danger,
.table-danger:focus {
  color: #fff;
  background-color: #ff4d4f;
  border-color: #ff4d4f;
}
.ant-modal-mask {
  background-color: rgba(0, 0, 0, 0.05);
}
.btnscoloras {
  background-color: #52c41a;
  border-color: #52c41a;
}
.ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  padding: 16px 14px;
}

.ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  padding: 16px 12px;
}
.clearfloat {
  margin-bottom: 18px;
}
#search .ant-select,
#search .ant-input-affix-wrapper {
  width: 142px;
  margin-right: 10px;
}
</style>